package wetalk.software.bupt.com.wetalk.view.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import wetalk.software.bupt.com.wetalk.GloableParams;
import wetalk.software.bupt.com.wetalk.R;
import wetalk.software.bupt.com.wetalk.application.UserManager;
import wetalk.software.bupt.com.wetalk.model.po.User;
import wetalk.software.bupt.com.wetalk.util.SharePreferenceUtil;
import wetalk.software.bupt.com.wetalk.view.activity.ChangePasswordActivity;
import wetalk.software.bupt.com.wetalk.view.activity.LoginActivity;
import wetalk.software.bupt.com.wetalk.view.activity.UpdateEmailActivity;
import wetalk.software.bupt.com.wetalk.view.activity.UpdatePhoneNumberActivity;

/**
 * Created by Administrator on 2017/12/5.
 */

public class Fragment_Profile extends Fragment  {
    private TextView mName;
    private TextView mDepartment;
    private TextView mEmail;
    private TextView mPhone;
    private ImageView mHead;
    private ImageButton mButtonChangePassword;
    private ImageButton mButtonUpdateEmail;
    private ImageButton mButtonUpdatePhoneNumber;
    private ImageButton mButtonExit;
    private View view;
    private Context mContext;
    private Activity mActivity;
    private User currentUser;//当前用户

    public static final String TAG="Fragment_Profile";



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.activity_my_info,container,false);
        mContext=getContext();
        mActivity=getActivity();
        //初始化布局
        initWidget();
        //展示用户信息
        userInfo(currentUser);
        //初始化button
        initEvent();


        return view;

    }


    //设置各个button的逻辑
    private  void initEvent(){
        //点击头像
        mHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });



        //修改密码按钮
        mButtonChangePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity,ChangePasswordActivity.class);
                intent.putExtra("extra_password",currentUser.getPassword());
                startActivity(intent);
            }
        });

        //用户更新手机号
        mButtonUpdatePhoneNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity,UpdatePhoneNumberActivity.class);
                intent.putExtra("extra_phone",GloableParams.PHONE);
                startActivity(intent);
            }
        });

        //用户更新邮箱
        mButtonUpdateEmail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity,UpdateEmailActivity.class);
                intent.putExtra("extra_email",GloableParams.EMAIL);
                startActivity(intent);
            }
        });

        //退出按钮
        mButtonExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
                dialog.setTitle("退出登录");
                dialog.setMessage("确认退出本帐号？");
                dialog.setCancelable(false);
                dialog.setPositiveButton("确定",new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharePreferenceUtil sharePreferenceUtil = new SharePreferenceUtil(getContext(), "WeTalk_SP");
                        sharePreferenceUtil.saveValue("no","isLogin");

                        SharePreferenceUtil sharePreferenceUtil2 = new SharePreferenceUtil(getContext(), "data");                       
                        sharePreferenceUtil2.saveValue("yes","isSendPull");
                        
                        Intent intent = new Intent(mActivity,LoginActivity.class);
                        startActivity(intent);
                        getActivity().finish();
                        Log.d(TAG,"退出登录成功");
                    }
                });
                dialog.setNegativeButton("取消",new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
            }
        });

    }

    //展示用户信息
    private  void userInfo(User user){
        GloableParams.PHONE = user.getPhone();
        GloableParams.EMAIL = user.getEmail();
        mName.setText(user.getUserName());
        mHead.setImageResource(R.mipmap.a3);
        mEmail.setText(GloableParams.EMAIL);
        mPhone.setText(GloableParams.PHONE);
        mDepartment.setText(user.getDepartment());
    }

    //初始化布局中各控件
    private void initWidget(){
        // 找到各控件
        mName = (TextView) view.findViewById(R.id.id_show_name);
        mHead = (ImageView) view.findViewById(R.id.id_head_portrait);
        mEmail = (TextView) view.findViewById(R.id.id_show_email);
        mPhone = (TextView) view.findViewById(R.id.id_show_phonenumber);
        mDepartment = (TextView) view.findViewById(R.id.id_show_department);

        //找到各个button
        mButtonChangePassword = (ImageButton) view.findViewById(R.id.id_change_password);
        mButtonUpdateEmail = (ImageButton) view.findViewById(R.id.id_bt_update_email);
        mButtonUpdatePhoneNumber = (ImageButton)view.findViewById(R.id.id_bt_update_phonenumber);
        mButtonExit = (ImageButton) view.findViewById(R.id.id_exit);


        //得到当前用户的信息
        currentUser = UserManager.getInstance(mContext).getCurrentUser();

     }

    public void refresh(){
        try {
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    mEmail.setText(GloableParams.EMAIL);
                    mPhone.setText(GloableParams.PHONE);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        refresh();
    }

}
